package com.qdxwx.manage.controller.colleges;

import com.qdxwx.common.config.UploadConfig;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.ImageUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.area.AreaService;
import com.qdxwx.data.service.colleges.CollegesService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.area.AreaFull;
import com.qdxwx.models.colleges.Colleges;
import com.qdxwx.models.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 合作院校Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/colleges")
public class CollegesEditController extends BaseController {

    @Autowired
    private CollegesService collegesService;
    @Autowired
    private AreaService areaService;

    @Permissions("colleges:edit")
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        Colleges colleges;
        AreaFull areaFull;
        if (id > 0) {
            colleges = collegesService.get(id);
            if (colleges == null) return error404;
            areaFull = areaService.getAreaFull(colleges.getAreaId());
        } else {
            colleges = new Colleges();
            areaFull = new AreaFull();
        }
        mmap.put("areaFull", areaFull);
        mmap.put("colleges", colleges);
        mmap.put("provinces", areaService.getChildren(0));
        return "colleges/colleges-edit";
    }

    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        Colleges colleges;
        if (id > 0) {
            colleges = collegesService.get(id);
            if (colleges == null) return AjaxResult.error("找不不到指定院校，id=" + id);
        } else {
            colleges = new Colleges();
        }
        String name = TKString.trim(request.getParameter("name"));
        if (TKString.isEmpty(name)) return AjaxResult.error("请输入院校名称");

        int areaId = TKConvert.toInteger(request.getParameter("areaId"));
        if (areaId < 1) {
           return AjaxResult.error("地区参数有误，areaId=" + areaId);
        }
        String address = TKString.trim(request.getParameter("address"));
        if (TKString.isEmpty(address)) {
            return AjaxResult.error("请输入院校详细地址");
        }
        String cover = TKString.trim(request.getParameter("cover"));
        if (TKString.isEmpty(cover)) {
            return AjaxResult.error("请上传院校封面图");
        }
        String contacts = TKString.trim(request.getParameter("contacts"));
        if (TKString.isEmpty(contacts)) {
            return AjaxResult.error("请输入联系人");
        }
        String mobile = TKString.trim(request.getParameter("mobile"));
        if (TKString.isEmpty(mobile)) {
            return AjaxResult.error("请输入联系人");
        }
        colleges.setName(name);
        colleges.setAreaId(areaId);
        colleges.setAddress(address);
        colleges.setIntro(TKString.trim(request.getParameter("intro")));
        colleges.setCover(cover);
        colleges.setDescribe(request.getParameter("describe"));
        colleges.setContacts(contacts);
        colleges.setMobile(mobile);
        colleges.setViewCount(TKConvert.toInteger(request.getParameter("viewCount")));
        colleges.setLikeCount(TKConvert.toInteger(request.getParameter("likeCount")));
        colleges.setIsElite(TKConvert.toInteger(request.getParameter("isElite")));
        colleges.setIsTop(TKConvert.toInteger(request.getParameter("isTop")));
        colleges.setStatus(TKConvert.toInteger(request.getParameter("status")));
        colleges.setLevelId(TKConvert.toInteger(request.getParameter("levelId")));
        //判断图片有没有修改
        if(!TKString.isEmpty(colleges.getCover())) {
            //原图
            String oldPath = colleges.getCover();
            //新图
            if (oldPath.contains("/temps/")) {
                String newPath = String.format("%s/%s", UploadConfig.uploadCoverThumb, oldPath.substring(oldPath.indexOf("/temps/") + 7));
                logger.info("oldPath:" + oldPath);
                logger.info("newPath:" + newPath);
                ImageUtils.zoomWithCut(UploadConfig.uploadRoot + oldPath, UploadConfig.uploadRoot + newPath, 700, 400);
                colleges.setCover(newPath);
            }
        }
        int row;
        if (id > 0) {
            row = collegesService.update(colleges);
        } else {
            row = collegesService.insert(colleges);
        }
        return row > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Log(title = "删除院校", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error();
        return toAjax(collegesService.delete(id));
    }

}
